package com.uiotsoft.small.http;

import cn.hutool.crypto.asymmetric.Sign;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.uiotsoft.util.HttpUtil;
import com.uiotsoft.util.decrypt.DecryptResponse;
import com.uiotsoft.util.encrypt.Md5;
import com.uiotsoft.util.encrypt.SignCore;
import com.uiotsoft.util.encrypt.small.Aes;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Data
@Slf4j
public class SmallHttp {
    private String appKey;
    private String appSecret;
    private String gateway ;

    public SmallHttp(String appKey, String appSecret, String gateway){
        this.appKey = appKey;
        this.appSecret = appSecret;
        this.gateway = gateway;
    }

    /**
     * 请求头的生成
     * @param method
     * @param data
     * @param token
     * @return
     */
    private Map<String, String> getHeader(String method,String data,String token){
        Map<String, String> headerMap = getCommonMap(method,data,token);
        headerMap.put("Content-Type","application/json");
        return headerMap;
    }

    private Map<String ,String> getCommonMap(String method,String data,String token){
        Map<String, String> allMap = new HashMap<>();
        Long timestamp = System.currentTimeMillis();
        if (token != null){
            allMap.put("token", token);
        }
        allMap.put("appkey", appKey);
        allMap.put("method", method);
        allMap.put("timestamp", timestamp+"");
        allMap.put("nonce", "11111");
        allMap.put("signType", "md5");
        allMap.put("encryptType", "AES");
        allMap.put("version", "1.0");
        allMap.put("isEncrypt", "true");
        if (data != null){
            allMap.put("data", data);
        }
        String sign = Md5.sign(SignCore.createLinkString(allMap),appSecret, "");
        //allMap.remove("data");
        allMap.put("sign",sign);
        return allMap;
    }

    /**
     * 发送post请求
     * @param method
     * @param unEncryptData
     * @param token
     * @return
     */
    public String requestPost(String method, String unEncryptData, String token){
        log.info("{}接口的发送数据为：\n------{}",method,unEncryptData);
        String encryptData = Aes.encryptAes(unEncryptData,appSecret);
        log.debug("加密后的数据为：{}",encryptData);
        Map<String, String> headerMap = getHeader(method,encryptData,token);
        log.debug("请求头为：{}",headerMap.toString());
        String result = HttpUtil.requestPost(gateway,headerMap,encryptData);
        log.debug("返回结果未解密：{}",result);
        String resultDecrypt = DecryptResponse.parseResponse(result,appSecret);
        log.info("{}接口的返回结果为：\n ------{}",method,resultDecrypt);
        return resultDecrypt;
    }

    public String requestGet(String method, String unEncryptData, String token){
        String gatewayNew;
        String encryptData = null;
        if (unEncryptData != null){
            log.info("接口{}的发送数据为：{}",method,unEncryptData);
            JSONObject dataJson = JSONUtil.parseObj(unEncryptData);
            Map<String,String> map = JSONUtil.toBean(dataJson,Map.class);
            encryptData = Aes.encryptAes(SignCore.createLinkString(map),appSecret);
            log.debug("加密后的数据为：{}",encryptData);
            Map<String, String> allMap = getCommonMap(method,encryptData,token);
            gatewayNew = gateway +"?"+ HttpUtil.createHttpString(allMap);
        }else {
            gatewayNew = gateway;
        }
        //String result = HttpUtil.requestGet(gateway,allMap);
        String result = HttpUtil.requestGet(gatewayNew);
        log.debug("返回结果未解密：{}",result);
        String resultDecrypt = DecryptResponse.parseResponse(result,appSecret);
        log.debug("{} 返回结果解密后为：{}",method,resultDecrypt);
        return resultDecrypt;
    }


}
